﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.AlbLab.GEP.Dao.Base;
using Com.AlbLab.GEP.Dao.Interface;
using Com.AlbLab.GEP.Domain.Entities;
using Oracle.DataAccess.Client;
using Com.AlbLab.GEP.Domain;
using System.Data;

namespace Com.AlbLab.GEP.Dao.Impl
{
    public class PraticaNOSTDDao : OracleDao<OracleSessionManager, ISessionModel>, IPraticaNOSTDDao
    {
        private string seletctAll = "select * from GEP_DATI_NOSTD";
        private string selectById = "select * from GEP_DATI_NOSTD WHERE NST_COD_PRATICA = :NST_COD_PRATICA";
        private string delete1 = "delete GEP_DATI_NOSTD WHERE DD01_COD_PRATICA = :DD01_COD_PRATICA";

        private string update1 = "UPDATE ASM_GEP.GEP_DATI_NOSTD"
                              + " SET DD01_DISATT_FUORI_ORARIO = :DD01_DISATT_FUORI_ORARIO, DD01_DT_NON_ESEGPRIMA = :DD01_DT_NON_ESEGPRIMA"
                              + " WHERE (DD01_COD_PRATICA = :DD01_COD_PRATICA)";

        private string insert = "INSERT INTO GEP_DATI_NOSTD"
                              + " (NST_COD_PRATICA, NST_POT_RICH_CONTR, NST_POT_RICH_TOLL, NST_FASI_RICH, NST_TENS_RICH, NST_USO_RICH, NST_RAGSOC_NEW, "
                              + "  NST_PIVA_NEW, NST_CODFIS_NEW, NST_IND_FORNIT_NEW, NST_NRO_PRA_RIF, NST_data_fine_forn, NST_non_eseguire_prima, NST_presenza_cliente_finale, NST_tipo_conn, NST_PREVENTIVO)"
                              + " VALUES(:NST_COD_PRATICA, :NST_POT_RICH_CONTR, :NST_POT_RICH_TOLL, :NST_FASI_RICH, :NST_TENS_RICH, :NST_USO_RICH, :NST_RAGSOC_NEW, "
                              + "  :NST_PIVA_NEW, :NST_CODFIS_NEW, :NST_IND_FORNIT_NEW, :NST_NRO_PRA_RIF, :NST_data_fine_forn, :NST_non_eseguire_prima, :NST_presenza_cliente_finale, :NST_tipo_conn, :NST_PREVENTIVO)";
        
        public PraticaNOSTDDao(OracleSessionManager daoContext)
            : base(daoContext)
        {
            if (!base.Context.Session_isActive())
            {
                base.Context.Session_Init();
                base.CurrentConnection.Open();
            }
        }

        private OracleParameter[] MappingParams(GEP_DATI_NOSTD r, bool isInsert)
        {
            OracleParameter[] oparams;
            oparams = new OracleParameter[16];

            oparams[0] = new OracleParameter("NST_COD_PRATICA", OracleDbType.Decimal, r.Id_Nst_Cod_Pratica , ParameterDirection.Input);
            oparams[1] = new OracleParameter("NST_POT_RICH_CONTR", OracleDbType.Decimal, r.Nst_Pot_Rich_Contr, ParameterDirection.Input);
            oparams[2] = new OracleParameter("NST_POT_RICH_TOLL", OracleDbType.Decimal, r.Nst_Pot_Rich_Toll, ParameterDirection.Input);
            oparams[3] = new OracleParameter("NST_FASI_RICH", OracleDbType.Decimal, r.Nst_Fasi_Rich, ParameterDirection.Input);
            oparams[4] = new OracleParameter("NST_TENS_RICH", OracleDbType.Decimal, r.Nst_Fasi_Rich, ParameterDirection.Input);
            oparams[5] = new OracleParameter("NST_USO_RICH", OracleDbType.Varchar2, r.Nst_Uso_Rich, ParameterDirection.Input);
            oparams[6] = new OracleParameter("NST_RAGSOC_NEW", OracleDbType.Varchar2, r.Nst_Ragsoc_New, ParameterDirection.Input);
            oparams[7] = new OracleParameter("NST_PIVA_NEW", OracleDbType.Varchar2, r.Nst_Piva_New, ParameterDirection.Input);
            oparams[8] = new OracleParameter("NST_CODFIS_NEW", OracleDbType.Varchar2, r.Nst_Codfis_New, ParameterDirection.Input);
            oparams[9] = new OracleParameter("NST_IND_FORNIT_NEW", OracleDbType.Varchar2, r.Nst_Ind_Fornit_New, ParameterDirection.Input);
            oparams[10] = new OracleParameter("NST_NRO_PRA_RIF", OracleDbType.Varchar2, r.Nst_Nro_Pra_Rif, ParameterDirection.Input);
            oparams[11] = new OracleParameter("NST_data_fine_forn", OracleDbType.Date, r.NST_data_fine_forn, ParameterDirection.Input);
            oparams[12] = new OracleParameter("NST_non_eseguire_prima", OracleDbType.Date, r.NST_non_eseguire_prima, ParameterDirection.Input);
            oparams[13] = new OracleParameter("NST_presenza_cliente_finale", OracleDbType.Varchar2, r.NST_presenza_cliente_finale, ParameterDirection.Input);
            oparams[14] = new OracleParameter("NST_tipo_conn", OracleDbType.Varchar2, r.NST_presenza_cliente_finale, ParameterDirection.Input);
            oparams[15] = new OracleParameter("NST_PREVENTIVO", OracleDbType.Varchar2, r.NST_presenza_cliente_finale, ParameterDirection.Input);
            //if (isInsert)
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Output);
            //else
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Input);
            return oparams;
        }

        #region IDao<GEP_DATI_NOSTD,decimal> Members

        public Dictionary<decimal, GEP_DATI_NOSTD> GetAll()
        {
            throw new NotImplementedException();
        }

        public GEP_DATI_NOSTD GetById(decimal id)
        {
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectById, CurrentConnection);
            cmd.Parameters.Add("NST_COD_PRATICA", id);
            GEP_DATI_NOSTD ut = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        ut = DaoOracleDbHelper.MapDatiNoStd(r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-D01003", e0x);
            }
            return ut;
        }

        public void Insert(GEP_DATI_NOSTD entity)
        {
            using (OracleCommand ocmd = new OracleCommand())
            {
                try
                {
                    ocmd.CommandText = insert;
                    ocmd.Parameters.AddRange(MappingParams(entity, false));
                    ocmd.Connection = base.CurrentConnection;
                    ocmd.ExecuteNonQuery();
                    // entity.Id_Tes_Cod_Pratica = (decimal)ocmd.Parameters["pID"].Value;
                }
                catch (Exception e0x)
                {
                    throw new ManagedException("EX-PR004", e0x);
                }
            }
        }

        public void Update(GEP_DATI_NOSTD entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(decimal id)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (!base.Context.TransactionModeOn)
            {
                base.CurrentConnection.Close();
            }
        }

        #endregion
    }
}